package org.jsping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsping.entity.Music;
import org.jsping.service.impl.MusicServiceImpl;

import com.alibaba.fastjson.JSON;

/**
 * 音乐管理  主要做接收用户想要的请求的歌曲数量,并返回回去
 * 这部分的数据主要是为了随心听页面服务
 */
@WebServlet("/MusicManager")
public class MusicManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
    public MusicManager() {
        super();
    }
    
    MusicServiceImpl msi = new MusicServiceImpl();
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String opr = request.getParameter("opr");
		String start = request.getParameter("start");
		String num = request.getParameter("num");
		if(opr.equals("lists") && num !=null) {
			//获取要请求的一定数量的歌曲,怎么去判断与之前的不同呢
			System.out.println("进来请求了,请求的起始数据是:"+start+",请求数量:"+num);
			Map<String, Music>musics = msi.getWannaMusics(new Integer(start), new Integer(num)); 
			String jsonStr = JSON.toJSONString(musics);
			PrintWriter out = response.getWriter();
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
